/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pcm.ui.client.actions;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
import org.opu.pc.ui.client.actions.AbstractDialogAction;
import org.opu.pc.ui.client.i18n.AbstractI18N;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.util.GUIUtils;
import org.opu.pcm.services.UserService;
import org.opu.pcm.services.beans.UserBean;
import org.opu.pcm.services.beans.UserBean.SecurityGroup;
import static org.opu.pcm.services.beans.UserBean.SecurityGroup.*;

/**
 *
 * @author yura
 */
public class AddUserUsersSettings extends AbstractDialogAction{
    
    protected UserService userService;
    protected List<SecurityGroup> list;

    public AddUserUsersSettings(String acionLabels) {
        super(acionLabels);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
        i18n = new AbstractI18N("org.opu.pcm.resource.i18n") {};
    }

    @Override
    public void action(ActionEvent e) throws Exception {
        super.action(e);
    }

	@Override
	protected void initProp(LinkedHashMap<String, Object> properties) {
		properties.put("user.name", "");
		properties.put("user.password", "");
		properties.put("user.email", "");
	}

	@Override
	protected void addPropertiesPane(JPanel cp, PropertiesEditPane pane) {
        list = getPrivilegesList();
        
        final JComponent listPane = new JPanel(new BorderLayout());
        Box v = Box.createVerticalBox();
        v.add(new ButtinWithId(i18n.getSafe("user.groups.Operator"), Operator, list));
		v.add(Box.createVerticalStrut(GUIUtils.DEFAULT_SPACE_HALF));
        v.add(new ButtinWithId(i18n.getSafe("user.groups.Secretary"), Secretary, list));
		v.add(Box.createVerticalStrut(GUIUtils.DEFAULT_SPACE_HALF));
        v.add(new ButtinWithId(i18n.getSafe("user.groups.Professor"), Professor, list));
		v.add(Box.createVerticalStrut(GUIUtils.DEFAULT_SPACE_HALF));
        listPane.add(v);
        
		Box b = Box.createVerticalBox();
		b.add(Box.createVerticalStrut(GUIUtils.DEFAULT_SPACE));
		b.add(pane);
		b.add(Box.createVerticalStrut(GUIUtils.DEFAULT_SPACE));
		b.add(inBordePanel(i18n.getSafe("user.groups.list") + ": ", listPane, true));
		
		cp.add(b);
	}

    @Override
    public String getName() {
        return "AddUser";
    }

    @Override
    protected OkAction getOkAction(JDialog dialog, PropertiesEditPane pane) {
        return new OkAction(pane, dialog) {
			private static final long serialVersionUID = 1L;

			@Override
			public void runAction(ActionEvent e) throws Exception {
                hideDialog();
				Map<String, Object> prop = pane.getProperties();
				String name = (String)prop.get("user.name");
				String password = (String)prop.get("user.password");
				String email = (String)prop.get("user.email");
                UserBean user = new UserBean(-1, name, password, email);
                user.setSecurityGroups(list);
				doOkAction(user);
                mainView.update();
			}
		};
    }

    @Override
    protected String getCacelActionName() {
        return CloseUsersSettings.MNAME;
    }

    protected void doOkAction(UserBean user) {
        userService.createUser(user);
    }

    protected List<SecurityGroup> getPrivilegesList() {
        return new ArrayList<SecurityGroup>();
    }
	
	private JPanel inBordePanel(String title, JComponent comp, boolean space){
		JPanel pane1 = new JPanel(new BorderLayout());
		pane1.setBorder(BorderFactory.createTitledBorder(title));
		if (space) {
			comp = GUIUtils.vBorder(GUIUtils.hBorder(comp));
		}
		pane1.add(comp, BorderLayout.CENTER);
		return pane1;
	}
    
    private class ButtinWithId<T> extends JCheckBox {
        
        private T item;
        private List<T> allItems;

        public ButtinWithId(String text, T item, List<T> items) {
            super(text);
            this.item = item;
            this.allItems = items;
            setSelected(items.contains(item));
            addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (ButtinWithId.this.isSelected()){
                        allItems.add(ButtinWithId.this.item);
                    } else {
                        allItems.remove(ButtinWithId.this.item);
                    }
                }
            });
        }
        
    }
    
}
